package com.starbaba.comment.view;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.comment.service.ActivityCommentService;
import com.starbaba.common.ResultJson;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.loanActivity.model.UserCommentModel;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.redis.JedisUtils;

@SuppressWarnings({"serial","unchecked","rawtypes","unused"})
public class ActivityCommentAction extends BaseAction implements ModelDriven<UserCommentModel> {

	@SuppressWarnings("deprecation")
    private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	private static final String COMM_REDIS_KEY = "loan:activity_comment_list_";   //评论列表在redis中key的前缀，跟loan_service中的获取活动评论列表要保持一致
	
	private ActivityCommentService service;

	@Autowired
	@Qualifier("activityCommentServiceImpl")
	public void setService(ActivityCommentService service) {
		this.service = service;
	}

	public String show(){
		return "success";
	}
	
	public void queryList(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		
		List list = service.getCommentList(start, length, pageResponse, paramsMap);
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());

		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void updateEffective(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		ResultJson resultJs = new ResultJson();
		JsonObject result = new JsonObject();
		String commentId = (String) paramsMap.get("commentId");
		String effective = (String) paramsMap.get("effective");
		String updateSql = "UPDATE loan_user_comment SET effective = ? WHERE id = ?";
		try {
			dao.update(updateSql, new Object[]{effective,commentId});
			
			//更新后需要删除缓存
			deleteCommInRedis(commentId,null);
			
			resultJs.msg ="修改成功";
		} catch (AppException e) {
			e.printStackTrace();
			resultJs.msg ="修改失败";
		}
		result.add("result", resultJs.toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//删除评论
	public void deleteComment(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String commentId = (String) paramsMap.get("commentId");
		String deleteSql = "DELETE FROM loan_user_comment WHERE id = ?";
		String selectSql = "SELECT * FROM loan_user_comment c WHERE c.id = ?";
		try {
			UserCommentModel comm = dao.get(selectSql, UserCommentModel.class, commentId);
			String activityId = comm.getActivity_id()+"";
			dao.delete(deleteSql, new Object[]{commentId});
			
			//更新后需要删除缓存
			deleteCommInRedis(commentId,activityId);
			
		} catch (AppException e) {
			e.printStackTrace();
		}
	}
	
	//删除缓存
	public void deleteCommInRedis(String commentId,String activityId) throws AppException{
		if(activityId == null || "".equals(activityId)){ //如果活动id为空，需要获取活动id
			String selectSql = "SELECT * FROM loan_user_comment c WHERE c.id = ?";
			UserCommentModel comm = dao.get(selectSql, UserCommentModel.class, commentId);
			if(comm == null){
				return;
			}
			activityId = comm.getActivity_id()+"";
		}
		String redisKey = COMM_REDIS_KEY+activityId;
		JedisUtils.del(redisKey);
	}
	
	@Override
	public UserCommentModel getModel() {
		
		return null;
	}

}





